local skill = fk.CreateSkill{
  name = "tea__tongzhi",
}
---@type ActiveSkillSpec
local tongzhi = {
  anim_type = "control",
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player and #to_select:getCardIds("h") > 0
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getCardIds("h") > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local targets = {player, target}
    local extraData = {
      num = 2,
      min_num = 2,
      include_equip = false,
      pattern = ".",
      reason = skill.name,
    }
    local prompt = "#askForTongzhi"
    local data = { "choose_cards_skill", prompt, false, extraData }
    local req = Request:new(targets, "AskForUseActiveSkill")
    req.focus_text = "#tea__tongzhi_considering"
    local req_table = {}
    for _, to in ipairs(targets) do
      if #to:getCardIds("h") > 1 then
        table.insert(req_table, to.id)
        req:setData(to, data)
      end
    end

    for _, p in ipairs(targets) do
      local cids = {}
      if table.contains(req_table, p.id) then
        if p.reply_ready then
          local replyCard = req:getResult(p).card
          cids = table.random(replyCard.subcards, 2)
        elseif #p:getCardIds("h") > 1 then
          cids = table.random(p:getCardIds(Player.Hand), 2)
        else
          cids = p:getCardIds("h")
        end
      else
        if #p:getCardIds("h") > 1 then
          cids = table.random(p:getCardIds(Player.Hand), 2)
        else
          cids = p:getCardIds("h")
        end
      end
      if cids then
        if p == player then
          target:addToPile("$tea__tongzhi", cids, false, skill.name, nil, {target.id, player.id})
        else
          player:addToPile("$tea__tongzhi", cids, false, skill.name, nil, {target.id, player.id})
        end
      end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local tongzhi_delay = {
  can_refresh = function(self, event, target, player, data)
    return target == player and #player:getPile("$tea__tongzhi") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local cids = player:getPile("$tea__tongzhi")
    room:obtainCard(player, cids, false, fk.ReasonJustMove)
  end,
}
skill:addEffect("active", tongzhi)
skill:addEffect(fk.TurnStart, tongzhi_delay)
return skill